home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / TBINDER.HPP < prev   
C/C++ Source or Header  |  1992-05-12  |  8KB  |  356 lines

  1. /*
  2.     TBinder.hpp -- Loose Data Binder v 1.7:
  3.         container class template.
  4.  
  5.     (C) Copyright 1992  John W. Small
  6.     All rights reserved
  7.  
  8.     PSW / Power SoftWare
  9.     P.O. Box 10072
  10.     McLean, Virginia 22102 8072 USA
  11.     (703) 759-3838
  12. */
  13.  
  14.  
  15. #ifndef TBinder_HPP
  16. #define TBinder_HPP
  17.  
  18.  
  19. #ifndef BINDER_HPP
  20. #include "binder.hpp"
  21. #endif
  22.  
  23. /*
  24.     To use the TBinder template, TYPE must have:
  25.  
  26.         default constructor
  27.         copy initializer constructor,
  28.             i.e. TYPE(TYPE&)
  29.         destructor
  30.         assignment operator, i.e. operator=()
  31.         ostream& operator<<(ostream&,TYPE&)
  32.         istream& operator>>(istream&,TYPE&)
  33. */
  34.  
  35.  
  36. template <class TYPE>
  37. class TBinder : Binder {
  38.  
  39.  
  40. protected:
  41.  
  42.     TBinder  (initVFTs)
  43.             : Binder(initVFTsOnly) {}
  44.     Binder:: destruct;
  45.     virtual  voiD Dassign(voiD D, const voiD S)
  46.             { (*(TYPE *)D) = (*(TYPE *)S);
  47.             return D; }
  48.     virtual  voiD Dnew(const voiD D)
  49.             { return  (voiD)
  50.             new TYPE(*(TYPE *)D); }
  51.     virtual  void Ddelete(voiD D)
  52.             { delete (TYPE *) D; }
  53.     Binder:: Dattach;
  54.     Binder:: Ddetach;
  55.     Binder:: sberror;
  56.     Binder:: berror;
  57.     virtual  void Dstore(ostream& os, voiD D)
  58.             { os << *(TYPE *)D << BDRendm; }
  59.     virtual  voiD Dload(istream& is)
  60.             { TYPE t; is >> t >> BDRnextm;
  61.             return (voiD) new TYPE(t); }
  62.     Binder:: store;
  63.     static   TBinder<TYPE> * load(istream& is,
  64.             TBinder<TYPE> * thiS);
  65.     Binder:: vload;
  66.     friend   ostream& operator<<(ostream&,
  67.             TBinder<TYPE>&);
  68.     friend   istream& operator>>(istream&,
  69.             TBinder<TYPE>*&);
  70.  
  71.  
  72. public:
  73.  
  74.  
  75.  
  76. /*  Constructors and destructor  */
  77.  
  78.     TBinder (unsigned flags = BDR_BIND_ONLY,
  79.         unsigned maxNodes = BDR_MAXNODES,
  80.         unsigned limit = BDR_LIMIT,
  81.         unsigned delta = BDR_DELTA) :
  82.         Binder(flags,maxNodes,limit,delta)
  83.         {}
  84.     TBinder    (TYPE * argv[],
  85.         unsigned argc = 0,
  86.         unsigned flags = BDR_BIND_ONLY)
  87.         : Binder((voiDV)argv,argc,flags) {}
  88.     TBinder (const char *filename)
  89.         : Binder(initVFTsOnly)
  90.         { (void) vload(filename,(BDRsloaD)
  91.         TBinder<TYPE>::load,this); }
  92.     Binder::save;
  93.     TYPE ** vector()
  94.             { return (TYPE **)Binder::
  95.             vector(); }
  96.     virtual ~TBinder()  { TBinder<TYPE>::destruct(); }
  97.  
  98.  
  99. /*  Housekeeping Primitives  */
  100.  
  101.     Binder:: Limit;
  102.     Binder:: setLimit;
  103.     Binder:: pack;
  104.     Binder:: Delta;
  105.     Binder:: setDelta;
  106.     Binder:: Nodes;
  107.     Binder:: MaxNodes;
  108.     Binder:: setMaxNodes;
  109.     Binder:: vacancy;
  110.     Binder:: vacancyNonElastic;
  111.     Binder:: Flags;
  112.     Binder:: setFlags;
  113.     Binder:: resetFlags;
  114.     TBinder<TYPE>&  operator<<(TBinder<TYPE>&
  115.             (*manipulator)(TBinder<TYPE>&))
  116.             { return (manipulator?
  117.             (*manipulator)
  118.             (*this)    : *this); }
  119.  
  120.  
  121.  
  122. /*  Elastic Array Primitives  */
  123.  
  124.     TYPE *   atIns(unsigned n, TYPE * D)
  125.             { return (TYPE *)Binder::
  126.             atIns(n,(voiD)D); }
  127.     TYPE *   atInsNew(unsigned n, const TYPE * D)
  128.             { return (TYPE *)Binder::
  129.             atInsNew(n,(const voiD)D); }
  130.     TYPE *   atRmv(unsigned n)
  131.             { return (TYPE *)Binder::
  132.             atRmv(n); }
  133.     Binder:: allRmv;
  134.     Binder:: atDel;
  135.     TYPE *   atDelAsg(unsigned n, TYPE * D)
  136.             { return (TYPE *)Binder::
  137.             atDelAsg(n,(voiD)D); }
  138.     Binder:: allDel;
  139.     TYPE *   atPut(unsigned n, TYPE * D)
  140.             { return (TYPE *)Binder::
  141.             atPut(n,(voiD)D); }
  142.     TYPE *   atPutNew(unsigned n, const TYPE * D)
  143.             { return (TYPE *)Binder::
  144.             atPutNew(n,(const voiD)D); }
  145.     TYPE *   atPutAsg(unsigned n, const TYPE * D)
  146.             { return (TYPE *)Binder::
  147.             atPutAsg(n,(const voiD)D); }
  148.     TYPE *   atGet(unsigned n)
  149.             { return (TYPE *)Binder::
  150.             atGet(n); }
  151.     TYPE *   operator[](unsigned n)
  152.             { return atGet(n); }
  153.     TYPE *   atGetAsg(unsigned n, TYPE * D)
  154.             { return (TYPE *)Binder::
  155.             atGetAsg(n,(voiD) D); }
  156.     TYPE *   atXchg(unsigned n, TYPE * D)
  157.             { return (TYPE *)Binder::
  158.             atXchg(n,(voiD) D); }
  159.     unsigned index(const TYPE * D)
  160.             { return Binder::
  161.             index((const voiD)D); }
  162.     Binder:: forEach;
  163.  
  164.  
  165. /*  Stack - Deque - Queue Primitives  */
  166.  
  167.     TYPE *   push(TYPE * D)
  168.             { return (TYPE *)Binder::
  169.             push((voiD) D); }
  170.     TYPE *   pushNew(const TYPE * D)
  171.             { return (TYPE *)Binder::
  172.             pushNew((const voiD)D); }
  173.     TYPE *   pop()
  174.             { return (TYPE *)Binder::
  175.             pop(); }
  176.     TBinder<TYPE>&  operator>>(TYPE *& D)
  177.             { D = atRmv(0); return *this; }
  178.     Binder:: popDel;
  179.     TYPE *   popDelAsg(TYPE * D)
  180.             { return (TYPE *)Binder::
  181.             popDelAsg((voiD)D); }
  182.     TYPE *   top()
  183.             { return (TYPE *)Binder::
  184.                 top(); }
  185.     TYPE *   topAsg(TYPE * D)
  186.             { return (TYPE *)Binder::
  187.             topAsg((voiD)D); }
  188.     TYPE *   insQ(TYPE * D)
  189.             { return (TYPE *)Binder::
  190.             insQ((voiD)D); }
  191.     TBinder<TYPE>&  operator<<(TYPE * D)
  192.             { atIns(Nodes(),D); return *this; }
  193.     TYPE *   insQNew(const TYPE * D)
  194.             { return (TYPE *)Binder::
  195.             insQNew((const voiD)D); }
  196.     TYPE *   unQ()
  197.             { return (TYPE *)Binder::
  198.             unQ(); }
  199.     Binder:: unQDel;
  200.     TYPE *   unQDelAsg(TYPE * D)
  201.             { return (TYPE *)Binder::
  202.             unQDelAsg((voiD)D); }
  203.     TYPE *   rear()
  204.             { return (TYPE *)Binder::
  205.                 rear(); }
  206.     TYPE *   rearAsg(TYPE * D)
  207.             { return (TYPE *)Binder::
  208.             rearAsg((voiD)D); }
  209.  
  210.  
  211. /*  List (single and double linked) Primitives  */
  212.  
  213.     Binder:: CurNode;
  214.     Binder:: setCurNode;
  215.     TYPE *   ins(TYPE * D)
  216.             { return (TYPE *)Binder::
  217.             ins((voiD)D); }
  218.     TYPE *   insNew(const TYPE * D)
  219.             { return (TYPE *)Binder::
  220.             insNew((const voiD)D); }
  221.     TYPE *   rmv()
  222.             { return (TYPE *)Binder::
  223.             rmv(); }
  224.     Binder:: del;
  225.     TYPE *   delAsg(TYPE * D)
  226.             { return (TYPE *)Binder::
  227.             delAsg((voiD)D); }
  228.     TYPE *   put(TYPE * D)
  229.             { return (TYPE *)Binder::
  230.             put((voiD)D); }
  231.     TYPE *   putNew(const TYPE * D)
  232.             { return (TYPE *)Binder::
  233.             putNew((const voiD)D); }
  234.     TYPE *   putAsg(const TYPE * D)
  235.             { return (TYPE *)Binder::
  236.             putAsg((const voiD)D); }
  237.     TYPE *   get()
  238.             { return (TYPE *)Binder::
  239.             get(); }
  240.     operator TYPE *()  { return get(); }
  241.     TYPE *   getAsg(TYPE * D)
  242.             { return (TYPE *)Binder::
  243.             getAsg((voiD)D); }
  244.     TYPE *   next()
  245.             { return (TYPE *)Binder::
  246.             next(); }
  247.     TYPE *   operator++()
  248.             { return next(); }
  249.     TYPE *   nextAsg(TYPE * D)
  250.             { return (TYPE *)Binder::
  251.             nextAsg((voiD)D); }
  252.     TYPE *   prev()
  253.             { return (TYPE *)Binder::
  254.             prev(); }
  255.     TYPE *   operator--()
  256.             { return prev(); }
  257.     TYPE *   prevAsg(TYPE * D)
  258.             { return (TYPE *)Binder::
  259.             prevAsg((voiD)D); }
  260.  
  261.     TYPE *   firstThat(BDRdetecT B, voiD M = voiD0)
  262.             { return (TYPE *)Binder::
  263.             firstThat(B,M); }
  264.     TYPE *   lastThat(BDRdetecT B, voiD M = voiD0)
  265.             { return (TYPE *)Binder::
  266.             lastThat(B,M); }
  267.  
  268.  
  269. /*  Priority Q, Set, Bag, Dictionary, Sort Primitives  */
  270.  
  271.     Binder:: Sorted;
  272.     Binder:: unSort;
  273.     Binder:: setComP;
  274.     Binder:: ComP;
  275.     Binder:: sort;
  276.     TYPE *   insSort(TYPE * D)
  277.             { return (TYPE *)Binder::
  278.             insSort((voiD)D); }
  279.     TYPE *   insSortNew(const TYPE * D)
  280.             { return (TYPE *)Binder::
  281.             insSortNew((const voiD)D); }
  282.     TYPE *   insUnique(TYPE * D)
  283.             { return (TYPE *)Binder::
  284.             insUnique((voiD)D); }
  285.     TYPE *   insUniqueNew(const TYPE * D)
  286.             { return (TYPE *)Binder::
  287.             insUniqueNew((const voiD)D); }
  288.     TYPE *   findFirst(const TYPE * K)
  289.             { return (TYPE *)Binder::
  290.             findFirst((const voiD)K); }
  291.     TYPE *   findNext(const TYPE * K)
  292.             { return (TYPE *)Binder::
  293.             findNext((const voiD)K); }
  294.     TYPE *   findLast (const TYPE * K)
  295.             { return (TYPE *)Binder::
  296.             findLast((const voiD)K); }
  297.     TYPE *   findPrev(const TYPE * K)
  298.             { return (TYPE *)Binder::
  299.             findPrev((const voiD)K); }
  300.     unsigned findAll(const TYPE * K)
  301.             { return Binder::
  302.             findAll((const voiD)K); }
  303.  
  304.  
  305.  
  306. };    /*  class TBinder  */
  307.  
  308.  
  309. template <class TYPE>
  310. TBinder<TYPE>* TBinder<TYPE>::load(istream& is,
  311.     TBinder<TYPE>* thiS)
  312. {
  313.     int newed;
  314.  
  315.     if (thiS)
  316.         newed = 0;
  317.     else  {
  318.         if ((thiS = new TBinder<TYPE>
  319.             (initVFTsOnly))
  320.             == voiD0)  {
  321.             sberror("unable to construct "
  322.                 "new TBinder for "
  323.                 "loading");
  324.             return (TBinder<TYPE> *)0;
  325.         }
  326.         newed = 1;
  327.     }
  328.     if (!Binder::load(is,(BindeR)thiS))  {
  329.         if (newed)
  330.             delete (voiD) thiS;
  331.         return (TBinder<TYPE> *)0;
  332.     }
  333.     return thiS;
  334. }
  335.  
  336.  
  337.  
  338. #define TBINDER(TYPE,TYPEBINDER,TYPEBINDERPTR)  \
  339.     inline ostream& operator<<(ostream& os,  \
  340.         TBinder<TYPE>& b)  \
  341.     {  \
  342.         if (b.Flags(BDR_DSTORE))  \
  343.             b.store(os);  \
  344.         return os;  \
  345.     }  \
  346.     inline istream& operator>>(istream& is,  \
  347.         TBinder<TYPE>*& B)  \
  348.         { B = TBinder<TYPE>::  \
  349.             load(is,(TBinder<TYPE> *)0);  \
  350.             return is; }  \
  351.     typedef TBinder<TYPE> TYPEBINDER;  \
  352.     typedef TYPEBINDER * TYPEBINDERPTR
  353.  
  354.  
  355. #endif  /*  TBinder_HPP  */
  356.